#ifndef _SCREEN_H_
#define _SCREEN_H_

#include <U8g2lib.h>

#ifdef U8X8_HAVE_HW_SPI
#include <SPI.h>
#endif
#ifdef U8X8_HAVE_HW_I2C
#include <Wire.h>
#endif


U8G2_SH1107_64X128_F_4W_HW_SPI u8g2(U8G2_R3, /* cs=*/ 14, /* dc=*/ 27, /* reset=*/ 33);

const uint8_t battery_7x12[118] U8G2_FONT_SECTION("battery_7x12") =
  "\5\0\4\4\4\3\1\1\5\14\7\0\0\7\0\0\0\0\0\0\0\0Y\60\15\374\71\254\10\31\42\344"
  ",C\254\0\61\22\374\71\254\10\31\42\272\21\341F\204\33\31b\5\0\62\22\374\71\254\10\31\42V#"
  "\242\32\21\325\310\20+\0\63\23\374\71\254\10\31\42nF\304fDlF\206X\1\0\64\23\374\71\254"
  "\10\31\42^\214\210\213\21q\61\62\304\12\0\0\0\0\4\377\377\0";

#define screen_bg_width 128
#define screen_bg_height 64
static const unsigned char screen_bg_bits[] U8X8_PROGMEM = {
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x00, 0x38, 0x04, 0x0F, 0xFE, 0x03, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x80, 0x7B, 0x20, 0x00, 0x00, 0x44, 0xC4, 0x30, 0x06,
  0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x89, 0x68, 0x00, 0x00,
  0x28, 0x25, 0x40, 0x8A, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x89, 0xB0, 0x00, 0x00, 0x10, 0x05, 0x0F, 0x52, 0x02, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x00, 0x79, 0x60, 0x00, 0x00, 0x50, 0x85, 0x10, 0x22,
  0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x29, 0xB0, 0x00, 0x00,
  0x50, 0x05, 0x00, 0x02, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x49, 0x68, 0x00, 0x00, 0x50, 0x05, 0x06, 0xFE, 0x03, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x80, 0x8B, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
  0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x80, 0x00, 0x00, 0x00, 0x00, 0x20, 0x02, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x00, 0x00, 0x00, 0x00,
  0xF8, 0x0F, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80,
  0x00, 0x88, 0x08, 0x00, 0x28, 0x0A, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x80, 0x00, 0x90, 0x04, 0x00, 0x08, 0x08, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x00, 0xC0, 0x01, 0x00,
  0xA8, 0x0A, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80,
  0x00, 0x20, 0x02, 0x00, 0x08, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x80, 0x00, 0x38, 0x0E, 0x00, 0xA8, 0x0A, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x00, 0x20, 0x02, 0x00,
  0x08, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80,
  0x00, 0xC0, 0x01, 0x00, 0xF8, 0x0F, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x80, 0x00, 0x90, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x00, 0x88, 0x08, 0x00,
  0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x80, 0x00, 0x00, 0x80, 0x01, 0x00, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x00, 0x01, 0x9C, 0x39,
  0xB8, 0x07, 0xE2, 0x38, 0x40, 0x38, 0x0E, 0x90, 0x8F, 0x00, 0x71, 0x9C,
  0x80, 0x01, 0x02, 0x44, 0x90, 0x08, 0x13, 0x45, 0x60, 0x04, 0x11, 0x18,
  0xC8, 0x80, 0x89, 0x82, 0x40, 0x11, 0x02, 0x04, 0x90, 0x08, 0x12, 0x41,
  0x40, 0x04, 0x11, 0x10, 0x84, 0x00, 0x81, 0x82, 0x2E, 0xA9, 0x1E, 0x04,
  0x90, 0x07, 0xE2, 0x39, 0x40, 0x3C, 0x0E, 0x10, 0x86, 0x00, 0x71, 0x9E,
  0xE0, 0x43, 0x22, 0x04, 0x90, 0x00, 0x02, 0x05, 0x40, 0x44, 0x11, 0x10,
  0x88, 0x00, 0x09, 0xA2, 0x00, 0x01, 0x22, 0x44, 0x90, 0x00, 0x02, 0x05,
  0x46, 0x44, 0x91, 0x91, 0x88, 0x18, 0x09, 0xA2, 0x00, 0x01, 0x1C, 0x38,
  0xB8, 0x00, 0xE7, 0x7C, 0xE6, 0x38, 0x8E, 0x39, 0xCF, 0x99, 0xFB, 0x9C,
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x00, 0x00, 0x00, 0x00,
  0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x80, 0x00, 0x00, 0x00, 0x00, 0x50, 0x01, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x00, 0x00, 0x00, 0x00,
  0x00, 0x80, 0xCF, 0x79, 0x3E, 0x07, 0x1C, 0x38, 0x88, 0x13, 0x45, 0x80,
  0x00, 0x00, 0x00, 0x00, 0xF4, 0x85, 0x20, 0x8A, 0xA0, 0x08, 0x22, 0x44,
  0x4C, 0xB4, 0x45, 0x80, 0x00, 0x00, 0x00, 0x00, 0x10, 0x81, 0x20, 0x88,
  0x10, 0x08, 0x3A, 0x40, 0x4A, 0x56, 0x45, 0x9F, 0x00, 0x00, 0x00, 0x00,
  0x14, 0x85, 0xC7, 0x79, 0x18, 0x07, 0x2A, 0x38, 0x49, 0x55, 0x7D, 0x88,
  0x00, 0x00, 0x00, 0x00, 0x10, 0x81, 0x00, 0x0A, 0xA0, 0x00, 0x1A, 0x04,
  0xDF, 0x14, 0x45, 0x84, 0x00, 0x00, 0x00, 0x00, 0xF4, 0x85, 0x20, 0x0A,
  0xA2, 0x00, 0x02, 0x04, 0x48, 0x14, 0x45, 0x82, 0x00, 0x00, 0x00, 0x00,
  0x00, 0x80, 0xCF, 0x09, 0xBC, 0x0F, 0x3C, 0x7C, 0x88, 0x13, 0x45, 0x9F,
  0x00, 0x00, 0x00, 0x00, 0x50, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x00, 0x00, 0x00, 0x00,
  0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
  0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x04, 0x00,
  0x00, 0x40, 0x00, 0x00, 0x40, 0x00, 0x00, 0x00, 0x00, 0xFE, 0x03, 0x04,
  0xE0, 0x03, 0x04, 0xF0, 0x1F, 0x40, 0x00, 0x08, 0x40, 0x00, 0x49, 0x00,
  0x00, 0x01, 0x04, 0x04, 0xF0, 0x07, 0x04, 0x10, 0x10, 0x40, 0x80, 0x28,
  0x40, 0x80, 0x4B, 0x00, 0x00, 0x09, 0x04, 0x04, 0x10, 0x04, 0x04, 0x50,
  0x10, 0x40, 0x40, 0x44, 0x40, 0x00, 0x49, 0x00, 0x00, 0x1D, 0x04, 0x04,
  0x10, 0x04, 0x04, 0x90, 0x10, 0x40, 0x20, 0x84, 0x40, 0x00, 0xE9, 0x00,
  0x00, 0x49, 0x05, 0x04, 0x38, 0x0E, 0x04, 0x50, 0x16, 0x40, 0x40, 0x44,
  0x40, 0x00, 0x49, 0x00, 0x00, 0x01, 0x04, 0x04, 0x38, 0x0E, 0x04, 0x10,
  0x10, 0x40, 0x80, 0x22, 0x40, 0x00, 0x5D, 0x00, 0x00, 0xFE, 0x03, 0x04,
  0x38, 0x0E, 0x04, 0xF0, 0x1F, 0x40, 0x00, 0x02, 0x40, 0x00, 0x49, 0x00,
  0x00, 0x00, 0x00, 0x04, 0x30, 0x06, 0x04, 0x00, 0x00, 0x40, 0x00, 0x00,
  0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x04, 0x00,
  0x00, 0x40, 0x00, 0x00, 0x40, 0x00, 0x00, 0x00, 0x3C, 0x22, 0xFA, 0x44,
  0xF4, 0x7C, 0xE4, 0x21, 0x9C, 0x48, 0x38, 0xCF, 0x47, 0xCF, 0xF3, 0x7D,
  0x22, 0x65, 0x0B, 0xC4, 0x16, 0x41, 0x24, 0x52, 0xA2, 0x48, 0x10, 0x51,
  0x40, 0x51, 0x14, 0x04, 0x82, 0xA8, 0x0A, 0x44, 0x15, 0x21, 0x24, 0x8A,
  0x82, 0x48, 0x10, 0x51, 0x40, 0x51, 0x14, 0x04, 0x82, 0xA8, 0x7A, 0x44,
  0xF5, 0x30, 0xE4, 0x89, 0x9C, 0x4F, 0x10, 0xD1, 0x43, 0xCF, 0xF3, 0x3C,
  0xB2, 0x2F, 0x0A, 0x44, 0x14, 0x40, 0x24, 0xFA, 0xA0, 0x48, 0x10, 0x51,
  0x40, 0x41, 0x11, 0x04, 0xA2, 0x28, 0x0A, 0x44, 0x14, 0x44, 0x24, 0x8A,
  0xA2, 0x48, 0x10, 0x51, 0x40, 0x41, 0x12, 0x04, 0xBC, 0x28, 0xFA, 0x44,
  0x14, 0x78, 0xE4, 0x89, 0x9C, 0x48, 0x38, 0xCF, 0x47, 0x41, 0xF4, 0x05,
  0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x04, 0x00, 0x00, 0x40, 0x00, 0x00,
  0x40, 0x00, 0x00, 0x00,
};


void drawScreenBackground() {
  uint8_t transparent = 0;
  if (!transparent) {
    u8g2.setBitmapMode(false /* solid */);
    u8g2.drawStr(0, 0, "Solid bitmap");
  } else {
    u8g2.setBitmapMode(true /* transparent*/);
    u8g2.drawStr(0, 0, "Transparent bitmap");
  }
  u8g2.setDrawColor(1); // White
  // u8g2.setDrawColor(0);// Black
  u8g2.drawXBMP(0, 0, screen_bg_width, screen_bg_height, screen_bg_bits);
  //  u8g2.drawXBMP(frame_size * 2, 24, cross_width, cross_height, cross_bits);
  //  u8g2.setDrawColor(2); // XOR
  //  u8g2.drawXBMP(frame_size * 3.5, 24, cross_width, cross_height, cross_bits);
}


void showSplashScreen(boolean isReset) {
  u8g2.clearBuffer();
  if (isReset) {
    u8g2.drawStr(10, 20, "INITIALIZING...");
  } else {
    u8g2.drawStr(10, 20, "LOADING...");
  }
  u8g2.sendBuffer();
}

void showConnectingWifi() {
  u8g2.clearBuffer();
  u8g2.drawStr(10, 20, "CONNECTING WIFI...");
  u8g2.sendBuffer();
}

void showWifiConnected() {
  u8g2.clearBuffer();
  u8g2.drawStr(10, 20, "WIFI CONNECTED, NTP..");
  u8g2.sendBuffer();
}

void drawCursor(uint8_t cursor_x, uint8_t cursor_y) {
  u8g2.drawTriangle(cursor_x, cursor_y, cursor_x, cursor_y + 9, cursor_x + 6, cursor_y + 6);
  u8g2.drawLine(cursor_x + 2, cursor_y + 3, cursor_x + 4, cursor_y + 11);
}

void drawTime() {
  struct tm timeinfo;
  if (!getLocalTime(&timeinfo)) {
    Serial.println("Failed to get local time, should not be here?");
  } else {
    char dateStringBuff[50]; //50 chars should be enough
    char timeStringBuff[50]; //50 chars should be enough
    char weekDayBuff[5]; //5 chars should be enough
    strftime(dateStringBuff, sizeof(dateStringBuff), "%Y-%m-%d", &timeinfo);
    strftime(weekDayBuff, sizeof(weekDayBuff), "%a", &timeinfo);
    strftime(timeStringBuff, sizeof(timeStringBuff), "%H:%M:%S", &timeinfo);
    // u8g2.setFont(u8g2_font_5x8_tf);
    u8g2.drawStr(15, 11, dateStringBuff);
    u8g2.drawStr(77, 11, weekDayBuff);
    u8g2.drawStr(40, 0, timeStringBuff);
  }
}

void drawSleepText() {
  u8g2.clearBuffer();
  u8g2.drawStr(10, 30, "Deep sleep now...");
  u8g2.sendBuffer();
}

void drawHelloStickWatch() {
  u8g2.drawStr(6, 6, "Hello,");
  u8g2.drawStr(6, 16, "DIY Stick Watch!");
  u8g2.drawStr(70, 1, reason);
}

uint8_t battery_index = 0;
unsigned int battery_ui_update_time = 0;

void drawBatteryStatusWithFont(bool isCharging, bool isChargeFull) {
  u8g2.setFont(battery_7x12);
  char * batteryIndexStr;
  if (isCharging && !isChargeFull) {
    // animation speed control
    unsigned long now = millis();
    if (now - battery_ui_update_time > 250) {
      battery_ui_update_time = now;
      battery_index++;
      if (battery_index > 4) {
        battery_index = 0;
      }
    }
    switch (battery_index) {
      case 0: batteryIndexStr = "0"; break;
      case 1: batteryIndexStr = "1"; break;
      case 2: batteryIndexStr = "2"; break;
      case 3: batteryIndexStr = "3"; break;
      case 4: batteryIndexStr = "4"; break;
      default: batteryIndexStr = "4"; break;
    }
  } else if (isChargeFull) {
    // charging and charge full!
    batteryIndexStr = "4";
  } else {
    // TODO: not charging
    batteryIndexStr = "3";
  }
  u8g2.drawStr(115, 0, batteryIndexStr);
  u8g2.setFont(NORMAL_FONT);
}

void drawBatteryStatus(bool isCharging, bool isChargeFull) {
  u8g2.setFont(u8g2_font_wqy12_t_gb2312);
  // u8g2.setFont(u8g2_font_5x7_mf);
  if (isCharging) {
    u8g2.drawUTF8(1, 51, "充电中/");
  } else {
    u8g2.drawUTF8(1, 51, "未充电/");
  }
  if (isChargeFull) {
    u8g2.drawUTF8(42, 51, "充满了!");
  } else {
    u8g2.drawUTF8(42, 51, "未充满");
  }
  u8g2.setFont(NORMAL_FONT);
}

void screenOffAnimation() {
  for (int i = 0; i < 32; i++) {
    u8g2.clearBuffer();
    u8g2.drawLine(0, i, 128, i);
    u8g2.drawLine(0, 64 - i, 128, 64 - i);
    u8g2.sendBuffer();
    delay(3);
  }
}

void drawChipTemprature(int temp_farenheit) {
  float temp_celsius = ( temp_farenheit - 32 ) / 1.8;
  char sensor_buf[64];
  sprintf(sensor_buf, "%.1fC", temp_celsius);
  u8g2.drawStr(98, 35, sensor_buf);
}

#endif // _SCREEN_H_
